package com.bugsnag.ohos;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * Notifier
 *
 * @since 2021-07-01
 */
public class Notifier implements JsonStream.Streamable {
    private String name;
    private String version;
    private String url;
    private List<Notifier> dependencies = new ArrayList<>();

    /**
     * constructor
     */
    public Notifier() {
        this("Ohos Bugsnag Notifier", "5.9.5", "https://bugsnag.com");
    }

    /**
     * constructor
     *
     * @param name
     * @param version
     * @param url
     */
    public Notifier(String name, String version, String url) {
        this.name = name;
        this.version = version;
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<Notifier> getDependencies() {
        return dependencies;
    }

    public void setDependencies(List<Notifier> dependencies) {
        this.dependencies = dependencies;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name("name").value(name);
        writer.name("version").value(version);
        writer.name("url").value(url);

        if (!dependencies.isEmpty()) {
            writer.name("dependencies");
            writer.beginArray();
            dependencies.forEach(new Consumer<Notifier>() {
                @Override
                public void accept(Notifier notifier) {
                    try {
                        writer.value(notifier);
                    } catch (IOException e) {
                        e.getMessage();
                    }
                }
            });
            writer.endArray();
        }
        writer.endObject();
    }
}
